package com.xjl.app.controller;

import com.xjl.app.EntityDTO.DeviceDTO;
import com.xjl.app.convert.DeviceDTODOConvert;
import com.xjl.app.domain.EntityDO.DeviceDO;
import com.xjl.app.domain.convert.DeviceDOConvert;
import com.xjl.app.infra.entity.Devices;
import com.xjl.app.infra.service.DevicesService;
import com.xjl.app.service.DeviceDTOService;
import com.xjl.common.web.Res.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("alarm/device")
@RequiredArgsConstructor
public class DeviceDTOController {

    private final DeviceDTOService deviceDTOService;
    private final DevicesService devicesService;
    @PostMapping("add")
    public Result addDevice(@RequestBody DeviceDTO deviceDTO){

        Devices save = deviceDTOService.save(deviceDTO);
        if(save!=null){
            return Result.ok(save);
        }
        log.error("添加物联网设备失败! \n data = {}", deviceDTO);
        return Result.error();
    }

    @GetMapping("/get")
    public Result getDevices(){
        try{
            List<Devices> list = devicesService.list();
            return Result.ok(list);
        }catch (Exception e){
            log.info(e.getMessage());
        }
        return Result.error();
    }

}
